#include "mainwindow.h"
#include "mydialog.h"
#include "ui_mydialog.h"
#include "server.h"


#include <QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

	MyDialog dlg;
	if ( QDialog::Accepted == dlg.exec() )
	{
		MainWindow w;
		w.show();

		Server s;
		s.init(dlg.ui->listenPort->value(), dlg.ui->rootIP->text(), dlg.ui->rootPort->value());

		w.setWindowTitle(dlg.ui->ipLabel->text() + ":" + QString::number(s.listenPort()));


		QObject::connect(&w, SIGNAL(painted(int, QPoint)), &s, SLOT(sendPaintMessage(int, QPoint)));
		QObject::connect(&s, SIGNAL(paint(int, QPoint)), &w, SLOT(paint(int,QPoint)));
		QObject::connect(&s, SIGNAL(appStateNeeded(QTcpSocket*)), &w, SLOT(onAppStateNeeded(QTcpSocket*)));
		QObject::connect(&w, SIGNAL(sceneState(QTcpSocket*,QList<QPair<int,QPoint> >)),
						 &s, SLOT(onStartupScene(QTcpSocket*,QList<QPair<int,QPoint> >)));
		QObject::connect(&w, SIGNAL(locked(bool)), &s, SLOT(sendLockMessage(bool)));
		QObject::connect(&s, SIGNAL(lockState(int)), &w, SLOT(setLock(int)));
		QObject::connect(&w, SIGNAL(lockState(QTcpSocket*,bool)), &s, SLOT(sendLock(QTcpSocket*,bool)));



		return a.exec();
	}
	else
	{
		return 0;
	}
}
